class WebSocket {

  connectSocket(url){
    if ("WebSocket" in window) {
      console.log("您的浏览器支持WebSocket");
      WebSocket._socketWorker(url) //创建WebSocket连接
    } else {
      console.log("您的浏览器不支持WebSocket");
    }
  }

  static _socketWorker(url) {
    let ws = new WebSocket(url);
    ws.onopen = function (evt) {
      WebSocket._openHandler(evt)
    }
    ws.onclose = function (evt) {
      WebSocket._closeHandler(evt)
    };
    ws.onmessage = function (evt) {
      WebSocket._messageHandler(evt)
    };
    ws.onerror = function (evt) {
      WebSocket._errorHandler(evt)
    };
  }


  static _openHandler(evt) {
    this.$notify({
      title: 'Success',
      message: '连接时间Socket服务器成功',
      type: 'success'
    });
  }

  static _closeHandler(evt) {
    console.log(evt)
  }

  static _messageHandler(evt) {
    console.log(evt)
  }

  static _errorHandler(evt) {
    console.log(evt)
  }
}

export default new WebSocket()
